﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace TuKu.PPS
{
    public partial class ppsAdlist : Form
    {
        protected int pkid = 0;
        public ppsAdlist()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int catid = cmbcateid.SelectedIndex;
            if (catid != 0)
            {
                string url = txtUrl.Text;
                string alt = txtAlt.Text;
                string img = txtImg.Text;
                StringBuilder sb = new StringBuilder();
                sb.Append(" insert into urladlist (url,alt,img,orderid,cateid) values ");
                sb.Append(" ('" + url + "','" + alt + "','" + img + "',999," + catid + " ) ");
                Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sb.ToString());
            }
            MessageBox.Show("添加成功");
            BindListView(cmbcateid.SelectedIndex);
        }
        private void btnModify_Click(object sender, EventArgs e)
        {
            string strsql = "update urladlist set url='" + txtUrl.Text + "',alt='" + txtAlt.Text + "',img='" + txtImg.Text + "' where id=" + pkid;
            Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(strsql);
            MessageBox.Show("更新完毕");
            BindListView(cmbcateid.SelectedIndex);
        }

        private void cmbcateid_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindListView(cmbcateid.SelectedIndex);
        }
        private void BindListView(int catid)
        {
            string strsql = "select * from urladlist where cateid="+catid;
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            listView1.Items.Clear();
            imageList1.Images.Clear();
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem item = new ListViewItem(dr["id"].ToString());
                //Image image = Image.FromStream(((HttpWebRequest)HttpWebRequest.Create(dr["img"].ToString())).GetResponse().GetResponseStream());
                Image image = Image.FromFile("d:\\tuku\\logo.jpg");
                imageList1.Images.Add(image);
                item.ImageIndex = i;
                i++;
                listView1.Items.Add(item);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                int id = int.Parse(listView1.SelectedItems[0].Text);
                pkid = id;
                DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query("select * from urladlist where id=" + id.ToString()).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    txtAlt.Text = dt.Rows[0]["alt"].ToString();
                    txtUrl.Text = dt.Rows[0]["url"].ToString();
                    txtImg.Text = dt.Rows[0]["img"].ToString();
                }

            }
        }


        
    }
}
